
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;


import messagepasser.DocInfo;
import messagepasser.UserInfo;

public class Client_Servant extends UnicastRemoteObject implements Client {
	
	LoggedIn mainWindow;
	int userid;
		
	public Client_Servant(LoggedIn i, int userid) throws RemoteException {
		mainWindow = i;
		this.userid = userid;
	}

	@Override
	public void newDocument(DocInfo d) throws RemoteException {
		System.out.println ("new user");
		
	}

	@Override
	public void newUserOnline(UserInfo u) throws RemoteException {
		for (UserInfo user : mainWindow.users) {
			if (user.getUserid() == u.getUserid()) {
				System.out.println("found");
				user.setIpaddr(u.getIpaddr());
				user.setOnlinestatus(u.isOnlinestatus());
				user.setPort(u.getPort());
				break;
			}
		}
		mainWindow.userNames.add(u.getUsername());
		mainWindow.getOnlineUsers().setListData(mainWindow.userNames);
		
	}	
	
	public void userOffline(UserInfo u) throws RemoteException {
		System.out.println ("logout called");
		
		for (UserInfo user : mainWindow.users) {
			if (user.getUserid() == u.getUserid()) {
				user.setIpaddr(null);
				user.setOnlinestatus(false);
				user.setPort(0);
				break;
			}
		}
		mainWindow.userNames.remove(u.getUsername());
		mainWindow.getOnlineUsers().setListData(mainWindow.userNames);
		
	}

	@Override
	public int getUserId() throws RemoteException {
		// TODO Auto-generated method stub
		return userid;
	}

	@Override
	public void acceptRequest(UserInfo dest)
			throws RemoteException {
	mainWindow.StartTxtChat(dest);
		
	}	
	
}
